package com.pawernode.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;


@Service
@Slf4j
public class MessageService {


    @Resource
    private RabbitTemplate rabbitTemplate;


    @Value("${my.exchangeName}")
    private String exchangeName;

    public void sendMsg() {
        // 创建一个消息对象，内容为"hello rabbitmq"
        Message message = MessageBuilder.withBody("hello rabbitmq".getBytes())
                .build();
        // 将消息发送到指定的交换机和路由键
        rabbitTemplate.convertAndSend(exchangeName, "info", message);
        // 记录日志信息，消息发送完毕的时间
        log.info("消息发送完毕，发送时间为：{}", new Date());
    }
}
